﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using BLL.Formation;
using BLL.SessionFormation;
using BLL.Formateur;
using AjaxControlToolkit;

namespace UI
{
    public partial class AddFormationPlanifie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DisplayFormDDL();
            }
        }

        protected void DisplayFormDDL()
        {
            try
            {
                (formAddFormationPlanifie.FindControl("ddlFormationName") as DropDownList).DataSource = FormationBLL.GetFormations();
                (formAddFormationPlanifie.FindControl("ddlFormationName") as DropDownList).DataBind();
                (formAddFormationPlanifie.FindControl("ddlFormateur") as DropDownList).DataSource = FormateurBLL.GetFomateurs();
                (formAddFormationPlanifie.FindControl("ddlFormateur") as DropDownList).DataBind();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        protected void bttAdd_OnClick(object sender, EventArgs e)
        {
            FormationBO formation = new FormationBO();
            SessionFormationBO bo = new SessionFormationBO();

            try
            {
                int FormationId = int.Parse((formAddFormationPlanifie.FindControl("ddlFormationName") as DropDownList).SelectedValue);
                int FormateurId = int.Parse((formAddFormationPlanifie.FindControl("ddlFormateur") as DropDownList).SelectedValue);

                bo.Formation = FormationBLL.GetFormations().FirstOrDefault(f => f.Identifiant == FormationId);
                bo.Formateur = FormateurBLL.GetFomateurs().FirstOrDefault(f => f.Identifiant == FormateurId);
                bo.DateDebut = DateTime.Parse((formAddFormationPlanifie.FindControl("tbDateDebut") as TextBox).Text.ToString());
                bo.DateFin = DateTime.Parse((formAddFormationPlanifie.FindControl("tbDateFin") as TextBox).Text);

                SessionFormationBLL.AddSessionFormations(bo);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            Response.Redirect("~/ManageCourses.aspx");
        }

        protected void bttCancel_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("~/ManageCourses.aspx");
        }
    }
}